
import { request } from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchList: [],
    isButton: false,
    inputValue: ""
  },
  sTime: -1,
  // 监听输入框
  handleSearch(e) {
    const { value } = e.detail
    // 检查合法性
    if (!value.trim()) {
      this.setData({
        searchList: [],
        isButton: false
      })
      return
    }
    this.setData({
      isButton: true
    })
    // 防抖
    clearTimeout(this.sTime)
    this.sTime = setTimeout(() => {
      this.getSearchList(value)
    }, 1000)
  },
  async getSearchList(query) {
    const res = await request({ url: '/goods/search', data: { query } })
    console.log(res.goods)
    this.setData({
      searchList: res.goods
    })
  },
  // 点击取消
  handleCancle() {
    this.setData({
      isButton: false,
      inputValue: "",
      searchList: []
    })
  }
})